package gui.component;

import gui.component.image.ResourceLoader;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.util.concurrent.Callable;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 *
 * @author Tom
 */
public class Work extends JPanel
{
      
    static final ImageIcon POINTER = new ImageIcon(ResourceLoader.class.getResource("pointer.png"));
    static final ImageIcon TICK = new ImageIcon(ResourceLoader.class.getResource("tick.png"));
    static final ImageIcon CROSS = new ImageIcon(ResourceLoader.class.getResource("cross.png"));
    static final ImageIcon PASSED = new ImageIcon(ResourceLoader.class.getResource("passed.png"));    
       
    JLabel image;
    JLabel text;

    public WorkStatus result;
    Callable<WorkStatus> method;
    String description;   
    int workID;

    public Work(Callable<WorkStatus> method, String description, int workID)
    {        
        this.workID=workID;

        this.setLayout(new FlowLayout(FlowLayout.LEFT));            
        image = new JLabel();
        image.setPreferredSize(new Dimension(25,25));
        text = new JLabel(description);
        Font f = this.getFont();
        text.setFont(f.deriveFont(f.getStyle() & ~Font.BOLD)); //unbold the text  
        this.add(image);
        this.add(text);

        this.method=method;
        this.description=description;        
        this.setEnabled(false);
    }

    public void start() throws Exception
    {
        Font f = this.getFont();            
        //graphics
        text.setFont(f.deriveFont(f.getStyle() | Font.BOLD)); //bold the text
        image.setIcon(POINTER);

        result=method.call();

        //graphics
        text.setFont(f.deriveFont(f.getStyle() & ~Font.BOLD)); //unbold the text          
        if (result.status==WorkStatus.Status.SUCCESS)
        {
            image.setIcon(TICK);
        }
        else if (result.status==WorkStatus.Status.FAIL)
        {
            image.setIcon(CROSS);
        }
        else
        {
            image.setIcon(PASSED);
        }
    }
}
